#/bin/bash

# dirs controls updating local directory
dirs=1
# runs controls whether code is run
runs=1

# set local path
# e.g., DIR="/Users/loudon/Dropbox/ethnicity/"
# DIR="/local/"

if [ $dirs = 1 ]; then

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd | sed 's|replicationPA||g')"

cd $DIR/replicationPA

mkdir AppendixTables/bak
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableI.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableIX.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableVII.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableVIII-2002.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableVIII-2004.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXI.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXII.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXIII-2002.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXIII-2004.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXIV.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXV.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXVI.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" AppendixTables/TableXVII.R

cd AppendixTables
mv *.bak bak/
cd bak
lst=$(ls *.bak)
for list in $lst; do
mv $list $(echo $list | sed 's|.bak||g')
done
cd ../..

mkdir Figure1/bak
sed -i.bak "s|path='/local/'|path='$DIR'|g" Figure1/Figure1a.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" Figure1/Figure1b.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" Figure1/Figure1c.R

cd Figure1
mv *.bak bak/
cd bak
lst=$(ls *.bak)
for list in $lst; do
mv $list $(echo $list | sed 's|.bak||g')
done
cd ../..

mkdir Figure2/bak
sed -i.bak "s|path='/local/'|path='$DIR'|g" Figure2/Figure2a.R
sed -i.bak "s|path='/local/'|path='$DIR'|g" Figure2/Figure2b.R

cd Figure2
mv *.bak bak/
cd bak
lst=$(ls *.bak)
for list in $lst; do
mv $list $(echo $list | sed 's|.bak||g')
done
cd ../..

mkdir Figure3/bak
sed -i.bak "s|path='/local/'|path='$DIR'|g" Figure3/Figure3.R

cd Figure3
mv *.bak bak/
cd bak
lst=$(ls *.bak)
for list in $lst; do
mv $list $(echo $list | sed 's|.bak||g')
done
cd ../..

mkdir Table1/bak
sed -i.bak "s|path='/local/'|path='$DIR'|g" Table1/Table1.R

cd Table1
mv *.bak bak/
cd bak
lst=$(ls *.bak)
for list in $lst; do
mv $list $(echo $list | sed 's|.bak||g')
done
cd ../..

mkdir Table2/bak
sed -i.bak "s|path='/local/'|path='$DIR'|g" Table2/Table2.R

cd Table2
mv *.bak bak/
cd bak
lst=$(ls *.bak)
for list in $lst; do
mv $list $(echo $list | sed 's|.bak||g')
done
cd ../..

fi

if [ $runs = 1 ]; then

if [ $DIR = "/local/" ]; then
	echo "Need to set local path";
else
	echo "Running replication files for 'Cause or Effect?'";

	nohup R < $DIR"replicationPA/Table1/Table1.R" > $DIR"replicationPA/Table1/Table1.Routs" --vanilla   
	nohup R < $DIR"replicationPA/Table2/Table2.R" > $DIR"replicationPA/Table2/Table2.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableI.R" > $DIR"replicationPA/AppendixTables/TableI.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableIX.R" > $DIR"replicationPA/AppendixTables/TableIX.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableVII.R" > $DIR"replicationPA/AppendixTables/TableVII.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableVIII-2002.R" > $DIR"replicationPA/AppendixTables/TableVIII-2002.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableVIII-2004.R" > $DIR"replicationPA/AppendixTables/TableVIII-2004.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXI.R" > $DIR"replicationPA/AppendixTables/TableXI.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXII.R" > $DIR"replicationPA/AppendixTables/TableXII.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXIII-2002.R" > $DIR"replicationPA/AppendixTables/TableXIII-2002.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXIII-2004.R" > $DIR"replicationPA/AppendixTables/TableXIII-2004.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXIV.R" > $DIR"replicationPA/AppendixTables/TableXIV.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXV.R" > $DIR"replicationPA/AppendixTables/TableXV.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXVI.R" >  $DIR"replicationPA/AppendixTables/TableXVI.Routs" --vanilla 
	nohup R < $DIR"replicationPA/AppendixTables/TableXVII.R" >  $DIR"replicationPA/AppendixTables/TableXVII.Routs" --vanilla 

	nohup R < $DIR"replicationPA/Figure1/Figure1a.R" > $DIR"replicationPA/Figure1/Figure1a.Routs" --vanilla 
	nohup R < $DIR"replicationPA/Figure1/Figure1b.R" > $DIR"replicationPA/Figure1/Figure1b.Routs" --vanilla 
	nohup R < $DIR"replicationPA/Figure1/Figure1c.R" > $DIR"replicationPA/Figure1/Figure1c.Routs" --vanilla 
	nohup R < $DIR"replicationPA/Figure2/Figure2a.R" > $DIR"replicationPA/Figure2/Figure2a.Routs" --vanilla 
	nohup R < $DIR"replicationPA/Figure2/Figure2b.R" > $DIR"replicationPA/Figure2/Figure2b.Routs" --vanilla  
	nohup R < $DIR"replicationPA/Figure3/Figure3.R" > $DIR"replicationPA/Figure3/Figure3.Routs" --vanilla   
fi
fi
# end 